package com.iweb.springboot_demo1.controller;

import cn.hutool.extra.mail.MailUtil;
import com.iweb.springboot_demo1.dto.DTOstu_email;
import com.iweb.springboot_demo1.pojo.Student;
import com.iweb.springboot_demo1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
//    @Autowired
//    private DTOstu_email dtostu_email;

    @GetMapping("students")
    public List<Student> getstudents() {
        return studentService.findallstu();
    }

    @PostMapping("students")
    public int poststudent(@RequestBody DTOstu_email dtostu_email) {
        String email_address = dtostu_email.getEmail();
        Student student = dtostu_email.getStudent();
        MailUtil.send(email_address,"注册成功","恭喜你注册成功",false);
        return studentService.insertstu(student);
    }

    @PutMapping("students/{id}")
    public int putstudent(@PathVariable long id, @RequestBody Student student) {
        student.setId(id);
        return studentService.updatestu(student);
    }

    @DeleteMapping("students/{id}")
    public int delstudent(@PathVariable int id) {
        return studentService.deletestu(id);
    }
}
